import os 
import json
import numpy as np
from tqdm import tqdm

img_idx = 46979
ann_idx = 94817


def process_one(data_path, ann_path, base_path):
    global img_idx, ann_idx
    image_list=[]
    ann_list=[]

    images = os.listdir(data_path)
    labels = np.load(ann_path)
    ts_idx = 0
    ts_last = labels[ts_idx][0]

    for idx in range(len(images)):
        img_name = images[idx]
        img={}
        img['id'] = img_idx
        img['width'] = 304
        img['height'] = 240
        img['file_name'] = os.path.join(base_path, img_name)
        image_list.append(img)

        for i in range(len(labels[ts_idx:])):
            ts, x, y, w, h, cls, _, _ = labels[ts_idx + i]
            ts, x, y, w, h, cls = int(ts), int(x), int(y), int(w), int(h), int(cls)
            if ts != ts_last:
                ts_idx = ts_idx + i
                ts_last = ts 
                break
            ann={}
            ann['id'] = ann_idx
            ann['image_id'] = img_idx
            ann['category_id'] = cls
            ann['iscrowd'] = 0
            ann['area'] = w*h
            ann['bbox'] = [x, y, w, h]
            ann['width'] = 304
            ann['height'] = 240
            ann_list.append(ann)

            ann_idx += 1
        img_idx += 1
    return image_list, ann_list


if __name__ == '__main__':
    # data_path = 'train/train_a/17-03-30_12-53-58_183500000_243500000'
    # ann_path = 'labels/train_ann/17-03-30_12-53-58_183500000_243500000_bbox.npy'
    # base_path = 'train_a/17-03-30_12-53-58_183500000_243500000'
    # x,y = process_one(data_path, ann_path, base_path)

    # json_path = 'train_f.json'
    # save_dic={}
    # image_list= []
    # ann_list = []
    # lost = []
    # data = 'train/train_f'
    # ann = 'labels/train_ann'
    # base = 'train_f'

    # videos = os.listdir(data)

    # for item in tqdm(videos):
    #     data_path = os.path.join(data, item)
    #     ann_name = item+'_bbox.npy'
    #     ann_path = os.path.join(ann, ann_name)
    #     base_path = os.path.join(base, item)

    #     if not os.path.exists(ann_path):
    #         lost.append(ann_path)
    #         continue

    #     x,y=process_one(data_path, ann_path, base_path)
    #     image_list.extend(x)
    #     ann_list.extend(y)
    
    # save_dic['images'] = image_list
    # save_dic['annotations'] = ann_list
    # print(lost,sep='\n')

    # f = open(json_path , 'w')
    # json.dump(save_dic, f)
    # print('img_idx =',img_idx)
    # print('ann_idx =', ann_idx)

    info = {"description": "Gen1 object detection Dataset", "year": "2024"}, 
    categories= [{"id": 1, "name": "people"}, {"id": 2, "name": "car"}]
    image_list= []
    ann_list = []

    f1 = open("test_a.json", 'r')
    dic_a = json.load(f1)
    image_list.extend(dic_a['images'])
    ann_list.extend(dic_a['annotations'])

    f2 = open("test_b.json", 'r')
    dic_b = json.load(f2)
    image_list.extend(dic_b['images'])
    ann_list.extend(dic_b['annotations'])

    # f3 = open("train_c.json", 'r')
    # dic_c = json.load(f3)
    # image_list.extend(dic_c['images'])
    # ann_list.extend(dic_c['annotations'])
    
    # f4 = open("train_d.json", 'r')
    # dic_d = json.load(f4)
    # image_list.extend(dic_d['images'])
    # ann_list.extend(dic_d['annotations'])
    
    # f5 = open("train_e.json", 'r')
    # dic_e = json.load(f5)
    # image_list.extend(dic_e['images'])
    # ann_list.extend(dic_e['annotations'])
    
    # f6 = open("train_f.json", 'r')
    # dic_f = json.load(f6)
    # image_list.extend(dic_f['images'])
    # ann_list.extend(dic_f['annotations'])

    save_dic={}
    save_dic['info'] = info
    save_dic['categories'] = categories
    save_dic['images'] = image_list
    save_dic['annotations'] = ann_list

    f = open('test.json', 'w')
    json.dump(save_dic, f)
    
            

        

